package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.GoodsCollectionDTO;
import top.zywork.query.GoodsCollectionQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.GoodsCollectionService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.GoodsCollectionVO;

import java.util.List;

/**
 * GoodsCollectionController控制器类<br/>
 *
 * 创建于2019-07-24<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/goods-collection")
@Slf4j
public class GoodsCollectionController extends BaseController {

    private GoodsCollectionService goodsCollectionService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存GoodsCollectionVO")
    public ResponseStatusVO save(@RequestBody @Validated GoodsCollectionVO goodsCollectionVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsCollectionService.save(BeanUtils.copy(goodsCollectionVO, GoodsCollectionDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存GoodsCollectionVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<GoodsCollectionVO> goodsCollectionVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsCollectionService.saveBatch(BeanUtils.copyListObj(goodsCollectionVOList, GoodsCollectionDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除GoodsCollectionVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        goodsCollectionService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除GoodsCollectionVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        goodsCollectionService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新GoodsCollectionVO")
    public ResponseStatusVO update(@RequestBody @Validated GoodsCollectionVO goodsCollectionVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = goodsCollectionService.update(BeanUtils.copy(goodsCollectionVO, GoodsCollectionDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新GoodsCollectionVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<GoodsCollectionVO> goodsCollectionVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        goodsCollectionService.updateBatch(BeanUtils.copyListObj(goodsCollectionVOList, GoodsCollectionDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结GoodsCollectionVO")
    public ResponseStatusVO active(@RequestBody GoodsCollectionVO goodsCollectionVO) {
        goodsCollectionService.update(BeanUtils.copy(goodsCollectionVO, GoodsCollectionDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结GoodsCollectionVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<GoodsCollectionVO> goodsCollectionVOList) {
        goodsCollectionService.updateBatch(BeanUtils.copyListObj(goodsCollectionVOList, GoodsCollectionDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        GoodsCollectionVO goodsCollectionVO = new GoodsCollectionVO();
        Object obj = goodsCollectionService.getById(id);
        if (obj != null) {
            goodsCollectionVO = BeanUtils.copy(obj, GoodsCollectionVO.class);
        }
        return ResponseStatusVO.ok("查询成功", goodsCollectionVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = goodsCollectionService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsCollectionVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody GoodsCollectionQuery goodsCollectionQuery) {
        PagerDTO pagerDTO = goodsCollectionService.listAllByCondition(goodsCollectionQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsCollectionVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody GoodsCollectionQuery goodsCollectionQuery) {
        PagerDTO pagerDTO = goodsCollectionService.listPageByCondition(goodsCollectionQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GoodsCollectionVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("user/pager-cond")
    public ResponseStatusVO userListPageByCondition(@RequestBody GoodsCollectionQuery goodsCollectionQuery) {
        goodsCollectionQuery.setUserId(SecurityUtils.getJwtUser().getUserId());
        return listPageByCondition(goodsCollectionQuery);
    }

    @PostMapping("user/collection")
    public ResponseStatusVO userCollection(@RequestBody GoodsCollectionVO goodsCollectionVO) {
        goodsCollectionVO.setUserId(SecurityUtils.getJwtUser().getUserId());
        goodsCollectionService.save(goodsCollectionVO);
        return ResponseStatusVO.ok("收藏成功", null);
    }

    @PostMapping("user/cancel-collection")
    @SysLog(description = "用户取消商品收藏")
    public ResponseStatusVO userCancelCollection(@RequestBody GoodsCollectionVO goodsCollectionVO) {
        Long deleteRow = goodsCollectionService.cancelCollection(SecurityUtils.getJwtUser().getUserId(), goodsCollectionVO.getGoodsId(), goodsCollectionVO.getGoodsSkuId(), goodsCollectionVO.getShopId());
        return ResponseStatusVO.ok("取消成功", null);
    }

    @GetMapping("user/remove/{id}")
    @SysLog(description = "用户删除商品收藏")
    public ResponseStatusVO userRemoveById(@PathVariable("id") Long id) {
        return removeById(id);
    }

    @Autowired
    public void setGoodsCollectionService(GoodsCollectionService goodsCollectionService) {
        this.goodsCollectionService = goodsCollectionService;
    }
}
